import {reqGetSearchInfo} from '@/api';
//search模块的小仓库
const state = {
  searchList: {}
};
const mutations = {
  SEARCHLIST(state, searchList) {
    state.searchList = searchList
  }
};
const actions = {
  //获取Search模块数据,commit是上下文提供的，params是调用方传递的，默认为{}
  async getSearchList({commit}, params = {}) {
    let result = await reqGetSearchInfo(params)
    if (result.code === 0) {
      commit("SEARCHLIST", result.data)
    }
  }
};
//计算属性，为了简化数据而生
const getters = {
  //state小仓库的state,计算属性，给一个初始值，以防网络故障
  //ES中查询到的商品
  products(state) {
    return state.searchList.products || []
  },
  //聚合品牌
  brands(state) {
    return state.searchList.brands || []
  },
  //聚合分类
  catalogs(state) {
    return state.searchList.catalogs || []
  },
  //聚合搜索得到的属性
  attrs(state) {
    return state.searchList.attrs || []
  },
  //前端传入的分析属性id
  attrIds(state) {
    return state.searchList.attrIds || []
  }
};
export default {
  state,
  mutations,
  actions,
  getters
}
